<template>
  <div class="pwd-form">
    <el-form
      status-icon
      label-width="100px"
      ref="pwdFormRef"
      :model="pwdForm"
      :rules="pwdFormRules"
    >
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input type="password" v-model="pwdForm.oldPwd"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="newPwd">
        <el-input type="password" v-model="pwdForm.newPwd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="pwdForm.checkPass"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('pwdFormRef')">提交</el-button>
        <el-button @click="resetForm('pwdFormRef')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.pwdForm.checkPass !== "") {
          this.$refs.pwdFormRef.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.pwdForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      pwdForm: {
        oldPwd: "",
        newPwd: "",
        checkPass: ""
      },
      pwdFormRules: {
        oldPwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ],
        newPwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          },
          { validator: validatePass, trigger: "blur" }
        ],
        checkPass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          },
          { validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { data: res } = await this.$http.post(
            "user/changePwd",
            this.pwdForm
          );

          console.log(res);

          if (res.code == 200) {
            this.$message({
              message: "密码修改成功",
              type: "success"
            });

          } else {

            this.$message({
              message: "密码修改失败",
              type: "info"
            });
          }

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$message("已重置");
    }
  }
};
</script>

<style lang="less" scoped>
.pwd-form {
  width: 20%;
  margin: auto;
  position: relative;
  top: 100px;
}
</style>>
